package com.twobu.twobuinsight.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.twobu.twobuinsight.common.Result;
import com.twobu.twobuinsight.dto.ChangePasswordRequest;
import com.twobu.twobuinsight.dto.UserPageRequest;
import com.twobu.twobuinsight.entity.User;
import com.twobu.twobuinsight.security.UserContext;
import com.twobu.twobuinsight.security.UserContextHolder;
import com.twobu.twobuinsight.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户相关接口")
@Deprecated
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/page")
    @Operation(summary = "分页查询用户列表")
    public Result<Page<User>> page(@RequestBody UserPageRequest request) {
        Page<User> page = new Page<>(request.getCurrent(), request.getSize());
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件，如果参数不为空
        if (StringUtils.hasText(request.getUsername())) {
            queryWrapper.like(User::getUsername, request.getUsername());
        }
        if (StringUtils.hasText(request.getRealName())) {
            queryWrapper.like(User::getRealName, request.getRealName());
        }
        if (StringUtils.hasText(request.getEmail())) {
            queryWrapper.like(User::getEmail, request.getEmail());
        }
        if (StringUtils.hasText(request.getPhone())) {
            queryWrapper.like(User::getPhone, request.getPhone());
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc(User::getCreateTime);

        return Result.success(userService.page(page, queryWrapper));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取用户")
    public Result<User> getById(@PathVariable Long id) {
        return Result.success(userService.getById(id));
    }

    @PostMapping
    @Operation(summary = "添加用户")
    public Result<Boolean> add(@RequestBody User user) {
        return Result.success(userService.save(user));
    }

    @PutMapping
    @Operation(summary = "更新用户")
    public Result<Boolean> update(@RequestBody User user) {
        return Result.success(userService.updateById(user));
    }

    @DeleteMapping("/{userId}")
    @Operation(summary = "删除用户")
    public Result<Boolean> delete(@PathVariable Long userId) {
        // 删除用户
        return Result.success(userService.removeById(userId));
    }

    @PutMapping("/current")
    @Operation(summary = "更新当前用户信息")
    public Result<Boolean> updateCurrentUser(@RequestBody User user) {
        // 获取当前登录用户
        UserContext userContext = UserContextHolder.getUserContext();
        if (userContext == null) {
            return Result.error("用户未登录");
        }

        // 查询当前用户
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserId, userContext.getUserId());
        User currentUser = userService.getOne(queryWrapper);

        if (currentUser == null) {
            return Result.error("用户不存在");
        }

        // 只允许更新部分字段
        currentUser.setRealName(user.getRealName());
        currentUser.setEmail(user.getEmail());
        currentUser.setPhone(user.getPhone());

        // 更新用户信息
        return Result.success(userService.updateById(currentUser));
    }

    @PutMapping("/change-password")
    @Operation(summary = "修改密码")
    public Result<Boolean> changePassword(@RequestBody ChangePasswordRequest request) {
        // 获取当前登录用户
        UserContext userContext = UserContextHolder.getUserContext();
        if (userContext == null) {
            return Result.error("用户未登录");
        }

        // 查询当前用户
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserId, userContext.getUserId());
        User currentUser = userService.getOne(queryWrapper);

        if (currentUser == null) {
            return Result.error("用户不存在");
        }

        // 验证旧密码
        if (!Objects.equals(request.getOldPassword(), currentUser.getPassword())) {
            return Result.error("当前密码不正确");
        }

        // 更新密码
        currentUser.setPassword(request.getNewPassword());

        // 更新用户信息
        return Result.success(userService.updateById(currentUser));
    }
}
